﻿#region Vương văn hùng
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;

namespace GUI.NhanVien
{
    public partial class frmXuLy : Form
    {
        #region Delegate
        public delegate void GetString(Boolean capnhat);
        public GetString GetData;
        public static bool CheckClose = false;
        #endregion

        #region Khoi Tao
        private DateTime date;
        private Boolean GioiTinh = false;
        private string TenPhongBan = string.Empty;
        private string MaPhongBan = string.Empty;
        private Entities.GetCombobox[] phongban = null;
        private string MaNhanVien = string.Empty;
        private string links = string.Empty;
        private string linksRoot = string.Empty;
        private Image image;
        private byte[] bytes;

        private string hanhDong;
        public string HanhDong
        {
            get { return hanhDong; }
            set { hanhDong = value; }
        }
        private Entities.NhanVien nhanvien;
        public Entities.NhanVien Nhanvien
        {
            get { return nhanvien; }
            set { nhanvien = value; }
        }
        public frmXuLy()
        {
            InitializeComponent();
        }
        public frmXuLy(string hanhDong, Entities.NhanVien nhanvien)
        {
            InitializeComponent();
            this.hanhDong = hanhDong;
            this.nhanvien = nhanvien;
        }
        #endregion

        #region Load
        private void frmXuLy_Load(object sender, EventArgs e)
        {
            try
            {
                PhongBan();
                date = Common.StaticVariable.InterfaceObject.SelectDate();
                switch (this.hanhDong)
                { 
                    case "Them":
                        MaNhanVien = this.GetID();
                        dpkNgaySinh.Text = date.Date.ToString();
                        dpkNgayCap.Text = date.Date.ToString();
                        break;
                    case "Sua":
                        DoDuLieu();
                        break;
                    default:
                        this.Thoat();
                        break;
                }
                this.WindowState = FormWindowState.Normal;
                txtTen.Focus();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Sua Du Lieu
        private void DoDuLieu()
        {
            try
            {
                MaNhanVien = nhanvien.MaNhanVien;
                MaPhongBan = nhanvien.MaPhongBan;
                TenPhongBan = nhanvien.TenPhongBan;
                HienThiPhongBan(nhanvien.MaPhongBan);
                GioiTinh = nhanvien.GioiTinh;
                if(nhanvien.GioiTinh == true)
                { rdoNam.Checked = true; }
                if (nhanvien.GioiTinh == false)
                {rdoNu.Checked = true;}
                txtTen.Text = nhanvien.TenNhanVien;
                txtDienThoaiBan.Text = nhanvien.DienThoaiCD;
                txtDienThoaiDiDong.Text = nhanvien.DienThoaiDD;
                txtEmail.Text = nhanvien.Email;
                txtDiaChiTamTru.Text = nhanvien.DCTamTru;
                txtDiaChiThuongTru.Text = nhanvien.DCThuongTru;
                txtLinkRoot.Text = nhanvien.LinkImageRoot;
                if (this.nhanvien.LinkImageRoot != string.Empty)
                {
                    bytes = Common.StaticVariable.InterfaceObject.GetImage(nhanvien.LinkImageRoot);
                    if (bytes != null)
                    {
                        image = Common.Utilities.GetImage(bytes);
                        picNhanVien.BackgroundImage = image;
                    }
                    else
                    {
                        bytes = Common.Utilities.GetImage(System.Windows.Forms.Application.StartupPath.ToString() + @"\Default.bmp");
                        image = Common.Utilities.GetImage(bytes);
                        picNhanVien.BackgroundImage = image;
                    }
                }
                else
                {
                    bytes = Common.Utilities.GetImage(System.Windows.Forms.Application.StartupPath.ToString() + @"\Default.bmp");
                    image = Common.Utilities.GetImage(bytes);
                    picNhanVien.BackgroundImage = image;
                }
                txtSoChungMinhThu.Text = nhanvien.CMND;
                txtNoiCap.Text = nhanvien.NoiCap;
                txtGhiChu.Text = nhanvien.GhiChu;
                dpkNgaySinh.Text = nhanvien.NgaySinh.Date.ToString();
                dpkNgayCap.Text = nhanvien.NgayCap.Date.ToString();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Thoát
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        CheckClose = true;
                        GetData(false);
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion

        #region Lay Ma
        private string GetID()
        {
            string code = string.Empty;
            try
            {
                Entities.GetID id = new Entities.GetID();
                id.TableName = "NhanVien";
                Entities.GetID get = Common.StaticVariable.InterfaceObject.SelectID(id);
                if (get.ColumnName != null)
                {
                    code = Common.Utilities.ProcessID(get.ColumnName);
                }
                else
                { code = "NV_0001"; }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                code = "NV_0001";
            }
            return code;
        }
        #endregion

        #region Phong Ban
        private void HienThiPhongBan(string maphongban)
        {
            try
            {
                for (int i = 0; i < phongban.Length; i++)
                {
                    if (phongban[i].TenCotKhoaChinh == maphongban)
                    {
                        cbxPhongBan.SelectedIndex = i;
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch (Exception)
            {
                cbxPhongBan.Items.Clear();
                MaPhongBan = string.Empty;
                TenPhongBan = string.Empty;
            }
        }
        private void PhongBan()
        {
            try
            {
                cbxPhongBan.Items.Clear();
                Entities.GetCombobox pb = new Entities.GetCombobox();
                pb.TenBang = "PhongBan";
                pb.TenCotKhoaChinh = "MaPhongBan";
                pb.TenCotTenHienThi = "TenPhongBan";
                phongban = Common.StaticVariable.InterfaceObject.SelectCombox(pb);
                Common.Utilities.XuLyComboBox(cbxPhongBan, phongban, "TenCotTenHienThi", "TenCotKhoaChinh");
            }
            catch (Exception ex)
            {
                cbxPhongBan.Items.Clear();
                string s = ex.Message;
            }
        }
        #endregion

        #region Quyen
        private Boolean Quyen()
        {
            return true;
        }
        #endregion

        #region Gia Tri Them
        private Entities.NhanVien LayBanGhi(string hanhdong)
        {
            Entities.NhanVien nv = new Entities.NhanVien();
            try
            {
                nv.HanhDong = hanhdong;
                nv.MaNhanVien = MaNhanVien;
                nv.TenNhanVien = txtTen.Text.Trim();
                nv.NgaySinh = DateTime.Parse(dpkNgaySinh.Text);
                nv.GioiTinh = GioiTinh;
                nv.TenPhongBan = this.TenPhongBan;
                nv.MaPhongBan = this.MaPhongBan;
                string codinh = txtDienThoaiBan.Text;
                if (codinh == "   -   -")
                {
                  codinh = string.Empty;
                }
                nv.DienThoaiCD = codinh;
                string didong = txtDienThoaiDiDong.Text;
                if (didong == "   -   -")
                {
                    didong = string.Empty;
                }
                nv.DienThoaiDD = didong;
                nv.Email = txtEmail.Text;
                nv.DCTamTru = txtDiaChiTamTru.Text;
                nv.DCThuongTru = txtDiaChiThuongTru.Text;
                nv.LinkImage = links;
                nv.LinkImageRoot = linksRoot;
                nv.Images = bytes;
                nv.CMND = txtSoChungMinhThu.Text;
                nv.NgayCap = DateTime.Parse(dpkNgayCap.Text);
                nv.NoiCap = txtNoiCap.Text;
                nv.GhiChu = txtGhiChu.Text;
                nv.Deleted = false;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                nv = null;
            }
            return nv;
        }
        #endregion

        #region Validate
        private Boolean Validate(Entities.NhanVien nv)
        {
            Boolean kt = true;
            try
            {
                if (nv.MaNhanVien == string.Empty)
                {
                    MessageBox.Show("Chưa có mã nhân viên","Thông báo");
                    kt = false;
                    return kt;
                }
                if (nv.TenNhanVien == string.Empty)
                {
                    MessageBox.Show("Nhập tên nhân viên", "Thông báo");
                    txtTen.Focus();
                    kt = false;
                    return kt;
                }
                if (DateTime.Compare(DateTime.Parse(dpkNgaySinh.Text).Date, date.Date) >= 0)
                {
                    MessageBox.Show("Ngày sinh phải nhỏ hơn ngày hiện tại", "Thông báo");
                    dpkNgaySinh.Focus();
                    kt = false;
                    return kt;
                }
                if (nv.TenPhongBan == string.Empty)
                {
                    MessageBox.Show("Nhập tên phòng ban", "Thông báo");
                    cbxPhongBan.Focus();
                    kt = false;
                    return kt;
                }
                if (nv.MaPhongBan == string.Empty)
                {
                    MessageBox.Show("Nhập mã phòng ban", "Thông báo");
                    cbxPhongBan.Focus();
                    kt = false;
                    return kt;
                }
                if (Common.ValidateControl.IsEmail(nv.Email)==false)
                {
                    MessageBox.Show("Định dạng email không đúng", "Thông báo");
                    txtEmail.Focus();
                    kt = false;
                    return kt;
                }
                if (nv.CMND == string.Empty)
                {
                    MessageBox.Show("Nhập số chứng minh thư", "Thông báo");
                    txtSoChungMinhThu.Focus();
                    kt = false;
                    return kt;
                }
                if (DateTime.Compare(DateTime.Parse(dpkNgayCap.Text).Date, date.Date) >= 0)
                {
                    MessageBox.Show("Ngày cấp phải nhỏ hơn ngày hiện tại", "Thông báo");
                    dpkNgayCap.Focus();
                    kt = false;
                    return kt;
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                kt = false;
            }
            return kt;
        }
        #endregion

        #region Tim Anh
        private void Search()
        {
            try
            {
                OpenFileDialog open = new OpenFileDialog();
                if (open.ShowDialog()== System.Windows.Forms.DialogResult.OK)
                {
                    links = open.SafeFileName.ToString();
                    linksRoot = open.FileName.ToString();
                    bytes = Common.Utilities.GetImage(linksRoot);
                    image = Common.Utilities.GetImage(bytes);
                    picNhanVien.BackgroundImage = image;
                    txtLinkRoot.Text = linksRoot;
                    picNhanVien.BackgroundImageLayout = ImageLayout.Stretch;
                }
                else
                {
                    links = this.nhanvien.LinkImage;
                    linksRoot = this.nhanvien.LinkImageRoot;
                    txtLinkRoot.Text = this.nhanvien.LinkImage;
                    bytes = this.nhanvien.Images;
                    image = this.nhanvien.Picture;
                }
                txtSoChungMinhThu.Focus();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Them
        private void Them()
        {
            try
            {
                if (this.Quyen() != true)
                {
                    MessageBox.Show("Bạn không có quyền thêm", "Thông báo");
                    return;
                }
                Entities.NhanVien nv = this.LayBanGhi("Insert");
                 this.MaNhanVien = this.GetID();
                 if (nv.MaNhanVien != this.MaNhanVien)
                {
                    nv = this.LayBanGhi("Insert");
                }
                 if (this.Validate(nv) == false)
                {
                    return;
                }
                int k = Common.StaticVariable.InterfaceObject.InsertUpdateNhanVien(nv);
                if (k == 0)
                {
                    MessageBox.Show("Thất bại hãy liện hệ với nhà quản trị", "Thông báo");
                    return;
                }
                CheckClose = true;
                GetData(true);
                this.Close();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Sua
        private void Sua()
        {
            try
            {
                if (this.Quyen() != true)
                {
                    MessageBox.Show("Bạn không có quyền sửa", "Thông báo");
                    return;
                }
                Entities.NhanVien nv = this.LayBanGhi("Update");
                //this.MaNhanVien = this.GetID();
                //if (nv.MaNhanVien != this.MaNhanVien)
                //{
                //    nv = this.LayBanGhi("Insert");
                //}
                if (this.Validate(nv) == false)
                {
                    return;
                }
                int k = Common.StaticVariable.InterfaceObject.InsertUpdateNhanVien(nv);
                if (k == 0)
                {
                    MessageBox.Show("Thất bại hãy liện hệ với nhà quản trị", "Thông báo");
                    return;
                }
                CheckClose = false;
                GetData(true);
                this.Close();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Event
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Thoat();
        }

        private void btnGhiLai_Click(object sender, EventArgs e)
        {
            try
            {
                switch (this.hanhDong)
                {
                    case "Them":
                        this.Them();
                        break;
                    case "Sua":
                        this.Sua();
                        break;
                }
            }
            catch (Exception)
            {
               
            }
        }

        private void txtTen_KeyPress(object sender, KeyPressEventArgs e)
        {
            Common.ValidateControl.CheckNumber(e);
        }

        private void txtSoChungMinhThu_KeyPress(object sender, KeyPressEventArgs e)
        {
            Common.ValidateControl.CheckString(e);
        }

        private void txtDienThoaiBan_KeyPress(object sender, KeyPressEventArgs e)
        {
            Common.ValidateControl.CheckString(e);
        }

        private void txtDienThoaiDiDong_KeyPress(object sender, KeyPressEventArgs e)
        {
            Common.ValidateControl.CheckString(e);
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            this.Search();
        }

        private void rdoNam_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoNam.Checked == true)
            {
                GioiTinh = true;
            }
            else
            {
                GioiTinh = false;
            }
        }

        private void rdoNu_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoNu.Checked == true)
            {
                GioiTinh = false;
            }
            else
            {
                GioiTinh = true;
            }
        }

        private void cbxPhongBan_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Entities.GetCombobox row = (Entities.GetCombobox)cbxPhongBan.SelectedItem;
                this.MaPhongBan = row.TenCotKhoaChinh;
                this.TenPhongBan = row.TenCotTenHienThi;
            }
            catch (Exception)
            {
                this.MaPhongBan = string.Empty;
                this.TenPhongBan = string.Empty;
            }
        }
        #endregion
    }
}
#endregion
